/*
   Copyright (c) 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2015-02-07 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.dli.utility;

import java.io.File;

public class DirectoryFactory {

	private static File dliDirectory = null;
	private static File dliArticlesDirectory = null;
	private static File dliDatabaseDirectory = null;
	private static File dliDatabaseIndexesDirectory = null;

	private DirectoryFactory() {} 
	
	public static File getDliDirectory() throws Exception {
		if (dliDirectory == null) { 
			String directory = System.getProperty("user.dir");
			if (directory == null){
				throw new Exception("System property [user.dir] is null.");
			}
			File file = new File(directory);
			if (!file.isDirectory()){
				throw new Exception("System property [user.dir]=" + directory + " is not a directory.");
			}
			directory = directory + File.separator + "mizartools" + File.separator + "dli";
			File f = new File(directory);
			if (!f.exists()) {
				if (!f.mkdirs()){
					throw new Exception("It is not possible to create the directory [" + directory + "].");
				};
			}
			dliDirectory = f;
		}
		return dliDirectory;
	}

	public static File getDliArticlesDirectory() throws Exception {
		if (dliArticlesDirectory == null) { 
			String directory = getDliDirectory().getAbsolutePath() + File.separator + "articles";
			File f = new File(directory);
			if (!f.exists()) {
				if (!f.mkdirs()){
					throw new Exception("It is not possible to create the directory [" + directory + "].");
				};
			}
			dliArticlesDirectory = f;
		}
		return dliArticlesDirectory;
	}
	
	public static File getDliDatabaseDirectory() throws Exception {
		if (dliDatabaseDirectory == null) { 
			String directory = getDliDirectory().getAbsolutePath() + File.separator + "database";
			File f = new File(directory);
			if (!f.exists()) {
				if (!f.mkdirs()){
					throw new Exception("It is not possible to create the directory [" + directory + "].");
				};
			}
			dliDatabaseDirectory = f;
		}
		return dliDatabaseDirectory;
	}

	public static File getDliDatabaseIndexesDirectory() throws Exception {
		if (dliDatabaseIndexesDirectory == null) { 
			String directory = getDliDatabaseDirectory().getAbsolutePath() + File.separator + "indexes";
			File f = new File(directory);
			if (!f.exists()) {
				if (!f.mkdirs()){
					throw new Exception("It is not possible to create the directory [" + directory + "].");
				};
			}
			dliDatabaseIndexesDirectory = f;
		}
		return dliDatabaseIndexesDirectory;
	}

}
